


#include "../../1_Application/Include/APP_WorkingNormalState.h"


#include "../../2_Process/Include/Cmp_Onboarding.h"


#include "../../3_Component/Include/Cmp_KeyScan.h"

#define __UARTCOMM__
#include "../../4_Library/Include/Lib_UartComm.h"
#undef __UARTCOMM__
#include "../../4_Library/Include/general.h"
#include "../../4_Library/Include/Int.h"
#include "../../4_Library/Include/Lib_CommSubMicom.h"
#include "../../4_Library/Include/Lib_SensingADC.h"
#include "../../4_Library/Include/Lib_EEPROM.h"


#include "../../5_WROS/Include/DDI_BootingDriver.h"
#include "../../5_WROS/Include/DDI_RegisterDriver.h"



#include "../../6_Vablesource/Include/Vable_ModelInformation.h"


extern const T_BufferDef mpstBufferInfo[CommStateMax][PlatformMax];
extern const UINT8 gaucBeeperInfo[PlatformMax][BeeperInformMax];
extern const UINT8 gauCommInfo[PlatformMax][CommInformMax];


UINT8 DPSendIndex = 0;
UINT8 DPRecvIndex = 0;

UINT8 PCSendIndex = 0;
UINT8 PCRecvIndex = 0;

UINT8 DPUartRece_Ok_F = 0;
UINT8 PCUartRece_Ok_F = 0;


T_Time gstDispSendGapTime;
T_Time gstDispRecvGapTime;
T_Time gstPCSendGapTime;




void Uart_Control()
{
	Uart_Display(gauCommInfo[Get_JigPlatform()][PacketGaptime],gunCommOper);
	Uart_PC(Jig2PCComm);
}
void Uart_Display(UINT8 ucSendGap, UINT8 ucCommOper)
{
	if(Timer_MsecCheckPassTime(&gstDispSendGapTime, ucSendGap) == SET)
	{
		UART_Control[ucCommOper].pSendFunc(ucCommOper);
	}
	else
	{;}

	if(Timer_MsecCheckPassTime(&gstDispRecvGapTime, 20) == SET)
	{

/********************************************

		NVIC_SetPriority(INTRX0_IRQn, 4);
		NVIC_EnableIRQ(INTRX0_IRQn);
		API_SIO_RecEnable(SC0, ENABLE);

		while(API_SIO_MOD2_Read(SC0) & 0x40)
		{
			API_SIO_RecvDataRead(SC0);
		}

********************************************/

		if(DPUartRece_Ok_F == SET)
		{
			DPUartRece_Ok_F = CLR;
			UART_Control[ucCommOper].pRecvFunc(ucCommOper);
		}
		else
		{;}
	}
	else
	{;}

}

void Uart_PC(UINT8 ucCommOper)
{

	if(Timer_MsecCheckPassTime(&gstPCSendGapTime, 200) == SET)
	{
		UART_Control[ucCommOper].pSendFunc(ucCommOper);
	}
	else
	{;}

	if(PCUartRece_Ok_F == SET)
	{
		PCUartRece_Ok_F = CLR;
		UART_Control[ucCommOper].pRecvFunc(ucCommOper);
	}
	else
	{;}

}

#define DP_UART
void Uart_DisplayPacketSendControl(UINT8 ucCommOper)
{
	DPSendIndex = 0;
	DPRecvIndex = 0;

	Comm_DispSetPacket_Normal(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer,ucCommOper);
	Uart_CrcPacketEncode(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer,\
						mpstBufferInfo[ucCommOper][Get_JigPlatform()].ucEtx);
	func_uart_disp_tx_packet(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer);
}

void Uart_DisplayPacketRecviveControl(UINT8 ucCommOper)
{
	Comm_DispRecvPacket_Normal(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstRecvBuffer);
}

#define OP_UART

void Uart_OptionPacketSendControl(UINT8 ucCommOper)
{
	DPSendIndex = 0;
	DPRecvIndex = 0;

	Comm_OptionSetPacket_Normal(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer,ucCommOper);
	Uart_CrcPacketEncode(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer,\
						mpstBufferInfo[ucCommOper][Get_JigPlatform()].ucEtx);
	func_uart_disp_tx_packet(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer);
}

void Uart_OptionPacketRecviveControl(UINT8 ucCommOper)
{
	Comm_OptionRecvPacket_Normal(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstRecvBuffer);
}


#define PC_UART

void Uart_PCPacketSendControl(UINT8 ucCommOper)
{
	PCSendIndex = 0;
	PCRecvIndex = 0;

	Comm_PCSetPacket_Normal(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer,ucCommOper);
	func_uart_pc_tx_packet(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer);
}

void Uart_PCPacketRecviveControl(UINT8 ucCommOper)
{
	Comm_PCRecvPacket_Normal(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstRecvBuffer);
}


#define MWI_UART


void MWI_UART_DispStartUartInterrupt(UINT8 ucCommOper, UINT8 ucInterruptMode)
{
	if(ucInterruptMode == 1)
	{
		func_uart_disp_rx_packet(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstRecvBuffer);
	}
	else if(ucInterruptMode == 2)
	{
		func_uart_disp_tx_packet(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer);
	}
	else
	{;}
}

void func_uart_disp_tx_packet(UINT8* pucPacket)
{
    /*
	if(DPSendIndex < pucPacket[(UINT8)(Get_JigPlatform() == Mega_Titan ? 0 : 1)])
	{
		API_SIO_SendDataSet(SC0,pucPacket[DPSendIndex++]);
	}
	else
	{
		DPSendIndex = 0;
	}
    */
}

void func_uart_disp_rx_packet(UINT8* pucPacket)
{
	pucPacket[1] = gauCommInfo[Get_JigPlatform()][RECV_DATA_SIZE];
/*
	if(DPRecvIndex < pucPacket[1])
	{	pucPacket[DPRecvIndex++] = API_SIO_RecvDataRead(SC0);
		if(DPRecvIndex >= pucPacket[1])
		{
			DPUartRece_Ok_F = SET;
		}
		else
		{;
		}
	}
	else
	{;
	}
	*/
}

void MWI_UART_PCStartUartInterrupt(UINT8 ucCommOper, UINT8 ucInterruptMode)
{
	if(ucInterruptMode == 1)
	{
		func_uart_pc_rx_packet(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstRecvBuffer);
	}
	else if(ucInterruptMode == 2)
	{
		func_uart_pc_tx_packet(mpstBufferInfo[ucCommOper][Get_JigPlatform()].pstSendBuffer);
	}
	else
	{;}
}


void func_uart_pc_tx_packet(UINT8* pucPacket)
{
	if(PCSendIndex < pucPacket[1])
	{
		//API_SIO_SendDataSet(SC2,pucPacket[PCSendIndex++]);
	}
	else
	{
		PCSendIndex = 0;
	}

}

void func_uart_pc_rx_packet(UINT8* pucPacket)
{
	pucPacket[1] = gauCommInfo[Get_JigPlatform()][RECV_DATA_SIZE];
/*
	if(PCRecvIndex < pucPacket[1])
	{	pucPacket[PCRecvIndex++] = API_SIO_RecvDataRead(SC2);
		if(PCRecvIndex >= pucPacket[1])
		{
			PCUartRece_Ok_F = SET;
		}
		else
		{;
		}
	}
	else
	{;
	}
	*/
}





